CREATE PROCEDURE spMyImport
(
	@Investor VARCHAR(100) = NULL,
	@Apn VARCHAR(50) = NULL,
	@PropertyLocation VARCHAR(400) = NULL,
	@DateRecorded VARCHAR(50) = NULL,
	@PurchasePrice VARCHAR(50) = NULL,
	@RehabCosts VARCHAR(50) = NULL,
	@CloseDate VARCHAR(50) = NULL,
	@NetSheet_PurchasePrice VARCHAR(50) = NULL, -- NOT USE
	@NetSheet_SellingPrice MONEY,
	@NetSheet_AcquisitionDate VARCHAR(50) = NULL, -- NOT USE
	@NetSheet_RehabCosts VARCHAR(50) = NULL, -- NOT USE
	@NetSheet_Occupancy VARCHAR(50) = NULL, -- NOT USE
	@NetSheet_EstClosingDate VARCHAR(50) = NULL,
	@PM VARCHAR(100) = NULL,
	@RehabStartDate VARCHAR(50) = NULL, --DATETIME = NULL,
	@EstRehabCompDate VARCHAR(50) = NULL, --DATETIME = NULL,
	@ActualCompDate VARCHAR(50) = NULL, --DATETIME = NULL,
	@CurrentRehabCosts VARCHAR(50) = NULL, -- NOT USE
	@RehabStatus VARCHAR(100) = NULL,
	@NotesStatus VARCHAR(MAX) = NULL,
	@RehabEstimate MONEY,
	@RehabMiscCosts MONEY,
	@RECORDED_F VARCHAR(50) = NULL, --DATETIME = NULL,
	@EffectiveDateBank VARCHAR(50) = NULL, --DATETIME = NULL,
	@ListingAgreement VARCHAR(50) = NULL, --DATETIME = NULL,
	@ContractDate VARCHAR(50) = NULL, --DATETIME = NULL,
	@ECOE_B VARCHAR(50) = NULL, --DATETIME = NULL,
	@Recorded_B VARCHAR(50) = NULL, --DATETIME = NULL,
	@BuyerPurchasePrice INT = NULL,
	@PurchaseType VARCHAR(50) = NULL,
	@Incentive VARCHAR(50) = NULL,
	@HOAManagement VARCHAR(100) = NULL,
	@MonthlyHOADues VARCHAR(100) = NULL,--INT = NULL,
	@PastDueHOA VARCHAR(50) = NULL,
	@ResalePacketOrdered VARCHAR(50) = NULL, --DATETIME = NULL,
	@ResalePkgToAgent VARCHAR(50) = NULL,
	@TitleCompanyBack VARCHAR(100) = NULL,
	@BuyerName VARCHAR(100) = NULL,
	@COOPAgent VARCHAR(100) = NULL,
	@Lender VARCHAR(100) = NULL
)
AS
BEGIN
	SET NOCOUNT ON
	SET DATEFORMAT MDY
	
	DECLARE @InvestorId INT, @PMId INT
		
	SELECT @InvestorId = UserId
	FROM UserAccounts 
	WHERE FName = @Investor
	
	IF (@InvestorId IS NULL)
	BEGIN
		INSERT INTO UserAccounts
		(Fname, LName, Email, password, Is_Active, Is_Deleted, IsAdmin, LvreoRole, SdreoRole, MasterListRole, Role, SubRole, CreatedByUser, IsVIP)
		VALUES (@Investor, '', '', '', 1, 0, 0, 'NA', 'NA', 'NA', 'IU', 'L', 0, 0)
		
		SET @InvestorId = SCOPE_IDENTITY()
	END
	
	SELECT @PMId = UserId
	FROM UserAccounts 
	WHERE FName = @PM
	
	IF (@PMId IS NULL)
	BEGIN
		INSERT INTO UserAccounts
		(Fname, LName, Email, password, Is_Active, Is_Deleted, IsAdmin, LvreoRole, SdreoRole, MasterListRole, Role, SubRole, CreatedByUser, IsVIP)
		VALUES (@PM, '', '', '', 1, 0, 0, 'NA', 'NA', 'NA', 'PM', 'L', 0, 0)
		
		SET @PMId = SCOPE_IDENTITY()
	END

	IF EXISTS(SELECT 1 FROM LvReo_MasterList WHERE Apn = @Apn)
		UPDATE LvReo_MasterList
		SET InvestorId = @InvestorId, PropertyLocation = @PropertyLocation, DateRecorded = @DateRecorded, PurchasePrice = @PurchasePrice,
			RehabCosts = @RehabCosts, CloseDate = @CloseDate, NetSheet_SellingPrice = @NetSheet_SellingPrice, NetSheet_EstClosingDate = @NetSheet_EstClosingDate,
			PMId = @PMId, RehabStartDate = @RehabStartDate, EstRehabCompDate = @EstRehabCompDate, ActualCompDate = @ActualCompDate, RehabStatus = @RehabStatus,
			NotesStatus = @NotesStatus, RehabEstimate = @RehabEstimate, RehabMiscCosts = @RehabMiscCosts, RECORDED_F = @RECORDED_F,
			EffectiveDateBank = @EffectiveDateBank, ListingAgreement = @ListingAgreement,
			ContractDate = @ContractDate, ECOE_B = @ECOE_B, Recorded_B = @Recorded_B, BuyerPurchasePrice = @BuyerPurchasePrice, PurchaseType = @PurchaseType,
			Incentive = @Incentive, HOAManagement = @HOAManagement, /*MonthlyHOADues = @MonthlyHOADues,*/ PastDueHOA = @PastDueHOA, ResalePacketOrdered = @ResalePacketOrdered, ResalePkgToAgent = @ResalePkgToAgent,
			TitleCompanyBack = @TitleCompanyBack, BuyerName = @BuyerName, COOPAgent = @COOPAgent, Lender = @Lender
		WHERE Apn = @Apn
	ELSE
		INSERT INTO LvReo_MasterList
		(Apn, InvestorId, PropertyLocation, DateRecorded, PurchasePrice, RehabCosts, CloseDate, 
		NetSheet_SellingPrice, NetSheet_EstClosingDate, PMId, RehabStartDate, EstRehabCompDate, ActualCompDate,
		RehabStatus, NotesStatus, RehabEstimate, RehabMiscCosts, RECORDED_F, EffectiveDateBank, ListingAgreement,
		ContractDate, ECOE_B, Recorded_B, BuyerPurchasePrice, PurchaseType,
		Incentive, HOAManagement, /*MonthlyHOADues,*/ PastDueHOA, ResalePacketOrdered, ResalePkgToAgent,
		TitleCompanyBack, BuyerName, COOPAgent, Lender)
		VALUES (@Apn, @InvestorId, @PropertyLocation, @DateRecorded, @PurchasePrice, @RehabCosts, @CloseDate, 
		@NetSheet_SellingPrice, @NetSheet_EstClosingDate, @PMId, @RehabStartDate, @EstRehabCompDate, @ActualCompDate,
		@RehabStatus, @NotesStatus, @RehabEstimate, @RehabMiscCosts, @RECORDED_F, @EffectiveDateBank, @ListingAgreement,
		@ContractDate, @ECOE_B, @Recorded_B, @BuyerPurchasePrice, @PurchaseType,
		@Incentive, @HOAManagement, /*@MonthlyHOADues,*/ @PastDueHOA, @ResalePacketOrdered, @ResalePkgToAgent,
		@TitleCompanyBack, @BuyerName, @COOPAgent, @Lender)
	
END
